/**
 * Copyright 2012 Alfredo "Rainbowbreeze" Morresi
 * 
 * This file is part of Eureka! project.
 * 
 * Eureka! is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Eureka! is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with Eureka!. If not, see http://www.gnu.org/licenses/.
 */
package it.rainbowbreeze.eureka.logic;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;

import it.rainbowbreeze.eureka.common.AppEnv;
import it.rainbowbreeze.eureka.domain.QualityDataPoint;
import it.rainbowbreeze.eureka.util.TestHelper;
import android.content.Context;
import android.test.AndroidTestCase;

/**
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class JsonConverterTest extends AndroidTestCase {
    // ------------------------------------------ Private Fields
    private JsonConverter mJsonConverter;

    // -------------------------------------------- Constructors

    // -------------------------------------- Setup and TearDown
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        Context context = getContext();
        assertNotNull("Context is null", context);
        TestHelper.init(context);
        mJsonConverter = AppEnv.i(getContext()).getJsonConverter();
    }

    
    // --------------------------------------------------- Tests
    
    public void testFromServerDataPointsInfo_NullString()
    throws JsonParseException, JsonMappingException, IOException {
        QualityDataPoint[] result;
        result = mJsonConverter.fromServerDataPointsInfo(null);
        assertNull("Wrong return value", result);
        result = mJsonConverter.fromServerDataPointsInfo("");
        assertNull("Wrong return value", result);
    }
    
    public void testFromServerDataPointsInfo_EmptyResponse()
    throws JsonParseException, JsonMappingException, IOException {
        QualityDataPoint[] result;
        result = mJsonConverter.fromServerDataPointsInfo("[]");
        assertNull("Wrong return value", result);
    }
    
    public void testFromServerDataPointsInfo()
    throws JsonParseException, JsonMappingException, IOException {
        String testString1 = "[{\"_id\":{\"$id\":\"4f8e07a7020211580a00029a\"},\"data_set\":\"DCCV_VERDURB\",\"type\":4,\"weight\":0.1,\"citta\":\"Italia\",\"anno\":{\"sec\":1262214000,\"usec\":0},\"geo\":[41.87194,12.56738],\"value\":{\"verde_densita\":9.3,\"verde_disponibilita\":105.85},\"normalized_score\":{\"verde_densita\":1,\"verde_disponibilita\":0},\"score\":2,\"anno_string\":2009},{\"_id\":{\"$id\":\"4f8e07a8020211580a000449\"},\"data_set\":\"DCCV_VERDURB\",\"type\":4,\"weight\":0.1,\"citta\":\"Roma\",\"anno\":{\"sec\":1262214000,\"usec\":0},\"geo\":[41.8905198,12.4942486],\"value\":{\"verde_densita\":27.46,\"verde_disponibilita\":131.36},\"normalized_score\":{\"verde_densita\":2,\"verde_disponibilita\":0},\"score\":1,\"anno_string\":2009},{\"_id\":{\"$id\":\"4f8e07a8020211580a000403\"},\"data_set\":\"DCCV_VERDURB\",\"type\":4,\"weight\":0.1,\"citta\":\"Terni\",\"anno\":{\"sec\":1262214000,\"usec\":0},\"geo\":[42.5602192,12.6467812],\"value\":{\"verde_densita\":21.76,\"verde_disponibilita\":410.39},\"normalized_score\":{\"verde_densita\":1,\"verde_disponibilita\":1},\"score\":1,\"anno_string\":2009},{\"_id\":{\"$id\":\"4f8e07a7020211580a000297\"},\"data_set\":\"DCCV_VERDURB\",\"type\":4,\"weight\":0.1,\"citta\":\"Viterbo\",\"anno\":{\"sec\":1262214000,\"usec\":0},\"geo\":[42.4173794,12.1048541],\"value\":{\"verde_densita\":1.28,\"verde_disponibilita\":82.92},\"normalized_score\":{\"verde_densita\":0,\"verde_disponibilita\":0},\"score\":0,\"anno_string\":2009}]";
        
        QualityDataPoint[] result = mJsonConverter.fromServerDataPointsInfo(testString1);
        assertNotNull("Wrong return value", result);
        assertEquals("Wrong array size", 4, result.length);
        QualityDataPoint dataPoint;
        
        dataPoint = result[0];
        assertEquals("Wrong measureId", 4, dataPoint.getMeasureId());
        assertEquals("Wrong latitude", 41871940, dataPoint.getLatitude());
        assertEquals("Wrong longitude", 12567380, dataPoint.getLongitude());
        assertEquals("Wrong score", 2, dataPoint.getScore());
        assertEquals("Wrong acquisition timestamp", 1262214000, dataPoint.getAcquisitionTimestamp());
        dataPoint = result[1];
        assertEquals("Wrong measureId", 4, dataPoint.getMeasureId());
        assertEquals("Wrong latitude", 41890519, dataPoint.getLatitude());
        assertEquals("Wrong longitude", 12494248, dataPoint.getLongitude());
        assertEquals("Wrong score", 1, dataPoint.getScore());
        assertEquals("Wrong acquisition timestamp", 1262214000, dataPoint.getAcquisitionTimestamp());
        dataPoint = result[2];
        assertEquals("Wrong measureId", 4, dataPoint.getMeasureId());
        assertEquals("Wrong latitude", 42560219, dataPoint.getLatitude());
        assertEquals("Wrong longitude", 12646781, dataPoint.getLongitude());
        assertEquals("Wrong score", 1, dataPoint.getScore());
        assertEquals("Wrong acquisition timestamp", 1262214000, dataPoint.getAcquisitionTimestamp());
        dataPoint = result[3];
        assertEquals("Wrong measureId", 4, dataPoint.getMeasureId());
        assertEquals("Wrong latitude", 42417379, dataPoint.getLatitude());
        assertEquals("Wrong longitude", 12104854, dataPoint.getLongitude());
        assertEquals("Wrong score", 0, dataPoint.getScore());
        assertEquals("Wrong acquisition timestamp", 1262214000, dataPoint.getAcquisitionTimestamp());
    }

    
    // ----------------------------------------- Private Methods

    // ----------------------------------------- Private Classes
}
